package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Question;
import cn.wolfcode.domain.QuestionItem;
import cn.wolfcode.domain.SaveQuestion;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IQuestionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Controller
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    private IQuestionService questionService;


    @RequiredPermission(name="问题页面",expression="question:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Question> pageInfo = questionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "question/list";
    }


    @RequiredPermission(name="问题删除",expression="question:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           questionService.delete(id);
        }
        return "redirect:/question/list";
    }


    @RequiredPermission(name="问题新增/编辑",expression="question:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Question question) {
        if (question.getId() == null) {
            questionService.save(question);
        } else {
            questionService.update(question);
        }
        return "redirect:/question/list";
    }


    @RequiredPermission(name="问题新增/编辑跳转",expression="question:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("question",questionService.get(id));
        }
        return "question/input";
    }

    @RequiredPermission(name="导出试题",expression="question:exportXls")
    @RequestMapping("/exportXls")
    public void exportXls(Integer[] typeArr, HttpServletResponse response) throws IOException {
        String fileName = new String("试题内容.xls".getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1);
        response.setHeader("Content-Disposition","attachment;filename=" + fileName);
        Workbook wb = questionService.exportXls(typeArr);
        wb.write(response.getOutputStream());
    }


    @RequiredPermission(name="导入试题",expression="question:importXls")
    @RequestMapping("/importXls")
    public String importXls(MultipartFile file){
        questionService.importXls(file);
        return "redirect:/question/list";
    }

    @RequiredPermission(name="获取选项集合",expression="question:queryItem")
    @RequestMapping("/queryItem")
    @ResponseBody
    public List<QuestionItem> queryItem(Long qid){
        List<QuestionItem> list = questionService.queryItem(qid);
        return list;
    }

    @RequiredPermission(name="录入单条问题",expression="question:insertQuestion")
    @RequestMapping("/insertQuestion")
    @ResponseBody
    public JsonResult insertQuestion(SaveQuestion saveQuestion){
        questionService.insertQuestion(saveQuestion);
        return new JsonResult();
    }

    @RequiredPermission(name="编辑问题",expression="question:updateQuestion")
    @RequestMapping("/updateQuestion")
    @ResponseBody
    public JsonResult updateQuestion(SaveQuestion saveQuestion){
        questionService.updateQuestion(saveQuestion);
        return new JsonResult();
    }
}
